package com.junit.src;

import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.hibernate.beans.Ptnt;


public class HibernateTest {
private SessionFactory sessionFactory;
private Session session = null;

@Before
public void before() {
}

@Test
public void returnsHerosWithMatchingType() {
	 System.out.println("Sarat => Configuring hibernate ... ");
	 Configuration cfg = new Configuration().configure();
	 System.out.println("Sarat => Done hibernate config... ");
	 
	 ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(
			 cfg.getProperties()).buildServiceRegistry();

	 System.out.println("Sarat => Building session factory ");
	 sessionFactory = cfg.buildSessionFactory(serviceRegistry);
	 
	 System.out.println("Sarat => Creating Patient...");
	 Ptnt ptnt = new Ptnt();
	 ptnt.setPtntFstNm("Boy");
	 ptnt.setPtntMdlNm("A");
	 ptnt.setPtntLstNm("Doe");
	 ptnt.setPtntDob(new Date());
	 ptnt.setPtntAge(28);
	 ptnt.setPtntSex("M");
	 ptnt.setPtntSpseFthrNm("Johny Jumbo");
	 ptnt.setPtntEduQual("Din't Edu Qual");
	 ptnt.setPtntOccuptn("No work");
	 ptnt.setPtntCmnts("Some comments");
	 ptnt.setCreatDttm(new Date());
	 ptnt.setCreatBy("Sarat");
	 ptnt.setModDttm(new Date());
 	 ptnt.setModBy("Sarat");
		
	 System.out.println("Sarat => Opening Session ");
	 session = sessionFactory.openSession();
	 
	 System.out.println("Sarat => Beginning Transaction");
	 Transaction tx = session.beginTransaction();
	 
	 System.out.println("Sarat => Saving to the session");
	 session.save(ptnt);
	 
	 System.out.println("Sarat => Committing transaction");
	 
	 System.out.println("Sarat => Done... session (before commit) = " + session);
	 
	 tx.commit();
	 
	 System.out.println("Sarat => Done... session (after commit)= " + session);
}

@After
public void after() {
}
}
